<!--  -->
<template>
    <div>
<div class="m-container">
    <div class="ui container m-pad-tb-big m-opacity ">
        <div class="ui segment" >
            <div id="blog-form" class="ui form">

                <div class="required two fields m-margin-tb-large">
                    <div class="field">
                        <div class="ui labeled input">
                            <div class="ui label basic teal">番剧名称</div>
                            <input type="text" v-model="anime.name">
                        </div>
                    </div>
                    <div class="field">
                        <div class="ui labeled input">
                            <div class="ui label basic teal">跳转链接</div>
                            <input type="text" v-model="anime.src">
                        </div>
                    </div>
                </div>
                <div class="required two fields m-margin-tb-large">
                    <div class="field">
                        <div class="ui labeled input">
                            <div class="ui label basic teal">开播时间</div>
                            <input type="date" v-model="anime.createTime">
                        </div>
                    </div>
                    <div class="field">
                        <div class="ui labeled input">
                            <div class="ui label basic teal">观看时间</div>
                            <input type="date" v-model="anime.watchTime">
                        </div>
                    </div>
                </div>
                <div class="field m-margin-tb-large">
                    <div class="ui labeled input">
                        <div class="ui label basic teal">展示图片</div>
                        <input type="text" v-model="anime.imgUrl">
                    </div>
                </div>
                <div class="ui negative message" v-show="error">
                    <i class="close icon" @click="closeMsg"></i>
                    <div class="header">提示:</div>
                    <p>{{message}}</p>
                </div>
                <div class="ui container right aligned">
                    <button type="button" class="ui button" onclick="window.history.go(-1)">返回</button>
                    <button class="ui teal submit button" @click="submit" v-if="id==-1">提交</button>
                    <button class="ui teal submit button" @click="submit" v-if="id!=-1">修改</button>
                </div>
            </div>
        </div>
    </div>
</div>
<br>
<br>
<br>

    </div>
</template>

<script>
export default {
    props:["id"],
    data () {
        return {
            error:false,
            message:"",
            anime:""
        };
    },
    methods:{
        submit(){
            let url
            if(this.id == -1){
                url="/admin/saveAnime"
            }else{
                url="/admin/updateAnime"
            }
            var _this = this
            _this.axios.post(url,{
                id:_this.id,
                name:_this.anime.name,
                src:_this.anime.src,
                imgUrl:_this.anime.imgUrl,
                createTime:_this.anime.createTime,
                watchTime:_this.anime.watchTime
            }).then(res =>{
                if(res.data.status === 200){
                    alert(res.data.msg)
                    _this.$router.push({path:"/admin/animes"})
                }else{
                    _this.message=res.data.msg
                    _this.error=true
                }
            }).catch(e =>{

            })
        },
        closeMsg(){
            this.error = false
        },
        formate(datetime) {
            function addDateZero(num) {
                return (num < 10 ? "0" + num : num);
            }
            let d = new Date(datetime);
            let formatdatetime = d.getFullYear() + '-' + addDateZero(d.getMonth() + 1) + '-' + addDateZero(d.getDate());
            return formatdatetime;
        },
    },
    created(){
        if(this.id==-1){
            this.anime={
                name:"",
                createTime:"",
                watchTime:"",
                src:"",
                imgUrl:""
            }
        }else{
            var _this = this
            _this.axios.get("/admin/getAnimeById",{
                params:{
                    id:_this.id
                }
            }).then(res=>{
                _this.anime = res.data.anime
                _this.anime.watchTime = this.formate(_this.anime.watchTime)
                _this.anime.createTime = this.formate(_this.anime.createTime)
            }).catch(e=>{
                console.log(e)
            })
        }
    }
    
}

</script>
<style lang='scss' scoped>
</style>